package com.lenovo.smarttraffic.zm_user_module;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.Test.TestDragItem.SwipeListLayout;
import com.lenovo.smarttraffic.bean.CollectionUserBean;
import com.lenovo.smarttraffic.database.dao.CollectionUserBeanDao;
import com.lenovo.smarttraffic.database.dao.DaoManager_Collection;
import com.lenovo.smarttraffic.database.dao.DaoManager_UserInfo;
import com.lenovo.smarttraffic.database.dao.UserInfoBean;
import com.lenovo.smarttraffic.database.dao.UserInfoBeanDao;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class User_centerActivity extends BaseActivity {
    private String TAG = "User_centerActivity";
    private RecyclerView rv_user_center;
    private UserInfoBeanDao userInfoBeanDao;
    private UserCenterAdapter userCenterAdapter;
    private List<UserInfoBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();


    }

    private void initData() {
        DaoManager_UserInfo.getmInstance_UserInfo().init(this);
        userInfoBeanDao = DaoManager_UserInfo.getmInstance_UserInfo().getDaoSession_UserInfo().getUserInfoBeanDao();
        list = userInfoBeanDao.queryBuilder().list();
        Log.e(TAG, "initData: " + list.size());


        userCenterAdapter = new UserCenterAdapter(this, list);
        rv_user_center.setAdapter(userCenterAdapter);

        userCenterAdapter.setOnLis(new UserCenterAdapter.OnLis() {
            @Override
            public void onTop(int position, DragLayout dragLayout) {
//                dragLayout.setStatus(DragLayout.Status.Close, true);
//                UserInfoBean zhiding = list.get(position);
//                list.remove(position);
//                userCenterAdapter.notifyItemRemoved(position);
//                userCenterAdapter.notifyItemRangeChanged(0, list.size() - position);
//                list.add(0, zhiding);
//                userCenterAdapter.notifyItemInserted(0);
//                userCenterAdapter.notifyItemRangeChanged(0, list.size() - 0);
//                Toast.makeText(getApplicationContext(), "置顶", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void insert(int position, DragLayout dragLayout) {
                DaoManager_Collection.getInstance_Collection().init(User_centerActivity.this);
                CollectionUserBeanDao collectionBeanDao = DaoManager_Collection.getInstance_Collection().getDaoSession_Collection().getCollectionUserBeanDao();
//防止重复插入

                List<CollectionUserBean> listQuery = collectionBeanDao.queryBuilder().where(CollectionUserBeanDao.Properties.Username.eq(User_centerActivity.this.list.get(position).getUsername())).list();
                if (listQuery.size() < 1) {
                    //当前时间
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String format = simpleDateFormat.format(date);

                    collectionBeanDao.save(new CollectionUserBean(null, User_centerActivity.this.list.get(position).getPname(), User_centerActivity.this.list.get(position).getPcardid(), User_centerActivity.this.list.get(position).getPsex(), User_centerActivity.this.list.get(position).getUsername(), User_centerActivity.this.list.get(position).getPtel(), User_centerActivity.this.list.get(position).getPregistdate(), User_centerActivity.this.list.get(position).getEmail(), format, false));
                    Toast.makeText(User_centerActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(User_centerActivity.this, "已收藏", Toast.LENGTH_SHORT).show();
                }
                Log.e(TAG, "insert: " + collectionBeanDao.queryBuilder().list().size() + "  ============ " + listQuery.size());
            }

            @Override
            public void delete(int position, DragLayout dragLayout) {
//                dragLayout.setStatus(DragLayout.Status.Close, true);
//                Toast.makeText(getApplicationContext(), "删除==" + "当前条目" + position, Toast.LENGTH_SHORT).show();
//                list.remove(position);
//                userCenterAdapter.notifyItemRemoved(position);
//                userCenterAdapter.notifyItemRangeChanged(0, list.size() - position);
            }
        });
    }

    private void initView() {
        initToolBar(findViewById(R.id.toolbar), true, "用户中心");
        rv_user_center = (RecyclerView) findViewById(R.id.rv_user_center);
        rv_user_center.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_user_center;
    }
}
